package com.anda.logistics.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.anda.logistics.dao.SystemRoleDao;
import com.anda.logistics.model.RoleMenuRights;
import com.anda.logistics.model.SystemRole;
import com.anda.logistics.model.SystemUser;
import com.anda.logistics.service.SystemRoleService;
import com.anda.logistics.util.JQGridPager;
import com.anda.logistics.vo.UserRoleVo;
@Component
public class SystemRoleServiceImpl implements SystemRoleService {
	private SystemRoleDao systemRoleDao;

	public SystemRoleDao getSystemRoleDao() {
		return systemRoleDao;
	}
	@Resource
	public void setSystemRoleDao(SystemRoleDao systemRoleDao) {
		this.systemRoleDao = systemRoleDao;
	}
	@Override
	public List<SystemRole> getPagerList(JQGridPager pager) {
		return systemRoleDao.getPagerList(pager);
	}
	@Override
	public long getPagerRecords(JQGridPager pager) {
		return systemRoleDao.getPagerRecords(pager);
	}
	@Override
	public void save(SystemRole sr) {
		systemRoleDao.save(sr);
	}
	@Override
	public void update(SystemRole sr) {
		systemRoleDao.update(sr);		
	}
	@Override
	public void deleteAll(String[] ids) {
		for(String sid: ids){
			SystemRole sr = systemRoleDao.getOneById(Integer.parseInt(sid));
			systemRoleDao.delete(sr);
		}
	}
	@Override
	public List<RoleMenuRights> getPagerList(JQGridPager pager, String tableName) {
		return systemRoleDao.getPagerList(pager,tableName);
	}
	@Override
	public long getPagerRecords(JQGridPager pager, String tableName) {
		return systemRoleDao.getPagerRecords(pager, tableName);
	}
	@Override
	public void save(RoleMenuRights rmr) {
		systemRoleDao.save(rmr);		
	}
	@Override
	public void update(RoleMenuRights rmr) {
		systemRoleDao.update(rmr);		
	}
	@Override
	public void deleteAllRoleMenu(String[] ids) {
		for(String sid: ids){
			RoleMenuRights rmr = systemRoleDao.getOneRoleMenuById(Integer.parseInt(sid));
			systemRoleDao.delete(rmr);
		}		
	}
	@Override
	public int checkUser(SystemUser su) {
		return systemRoleDao.checkUser(su);
	}
	@Override
	public List<UserRoleVo> getUserRolePagerList(JQGridPager pager, SystemUser su) {
		return systemRoleDao.getUserRolePagerList(pager, su);
	}
	@Override
	public long getUserRolePagerRecords(JQGridPager pager, SystemUser su) {
		return systemRoleDao.getUserRolePagerRecords(pager, su);
	}
	@Override
	public void saveRoleGrant(long userId, String[] roleIds) {
		for(String ids: roleIds){
			if ( systemRoleDao.getGrantedUserRoleCount(userId, Long.parseLong(ids)) == 0){
				systemRoleDao.grantRole(userId, Long.parseLong(ids));
			}
		}		
	}
	@Override
	public SystemUser getSystemUser(String userId) {
		return systemRoleDao.getSystemUser(userId);
	}
	
	
}
